﻿/******************************************************************************
文件名称    :   MySqlMensesDataProvider.cs
创建日期    :   2011-4-23 17:55:20
功能描述    :   病人月经信息实体数据提供程序MySql实现类
修改日志    :   
    2011-04-23 17:55  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// 病人月经信息实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlMensesDataProvider : MensesDataProvider
    {
        public MySqlMensesDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, Menses entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_menses(patient_id, has_menses, menses_cycle, Amenorrhea_time) values(?patient_id, ?has_menses, ?menses_cycle, ?Amenorrhea_time);";
                GenericDbHelper.AddParameter(mgr, cmd, "?patient_id", DbType.UInt32).Value = entity.PatientId;
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16).Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32).Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32).Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = (UInt32)reader.GetInt32(0);
                        //entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<Menses> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_menses(id, patient_id, has_menses, menses_cycle, Amenorrhea_time) values(?id, ?patient_id, ?has_menses, ?menses_cycle, ?Amenorrhea_time);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.UInt32);
                GenericDbHelper.AddParameter(mgr, cmd, "?patient_id", DbType.UInt32);
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16);
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32);

                //执行数据库操作
                foreach (Menses entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.PatientId;
                    cmd.Parameters[i++].Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                    cmd.Parameters[i++].Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                    cmd.Parameters[i++].Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;
                    cmd.ExecuteNonQuery();
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, Menses entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_menses set patient_id = ?patient_id, has_menses = ?has_menses, menses_cycle = ?menses_cycle, Amenorrhea_time = ?Amenorrhea_time where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?patient_id", DbType.UInt32).Value = entity.PatientId;
                GenericDbHelper.AddParameter(mgr, cmd, "?has_menses", DbType.UInt16).Value = entity.HasMenses == null ? DBNull.Value : (object)entity.HasMenses.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?menses_cycle", DbType.Int32).Value = entity.MensesCycle == null ? DBNull.Value : (object)entity.MensesCycle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?Amenorrhea_time", DbType.Int32).Value = entity.AmenorrheaTime == null ? DBNull.Value : (object)entity.AmenorrheaTime.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.UInt32).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        //entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, uint key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //删除SQL语句
                cmd.CommandText = "delete from tb_menses where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.UInt32).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/
                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_menses {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override Menses Get(TransactionManager manager, uint key)
        {
            Menses entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_menses where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.UInt32).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<Menses> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<Menses> list = new List<Menses>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_menses {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_menses {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_menses {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override Menses Fill(IDataReader reader, Menses entity)
        {
            if (entity == null) entity = new Menses();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = (UInt32)reader.GetInt32(i);
                        break;
                    case "patient_id":
                        entity.PatientId = (UInt32)reader.GetInt32(i);
                        break;
                    case "has_menses":
                        entity.HasMenses = reader[i] == DBNull.Value ? entity.HasMenses : reader.GetBoolean(i);
                        break;
                    case "menses_cycle":
                        entity.MensesCycle = reader[i] == DBNull.Value ? entity.MensesCycle : reader.GetInt32(i);
                        break;
                    case "Amenorrhea_time":
                        entity.AmenorrheaTime = reader[i] == DBNull.Value ? entity.AmenorrheaTime : reader.GetInt32(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }
    }
}
